package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.comment.domain.ItemComment;
import cn.wolfcode.luowowo.comment.query.ItemCommentQuery;
import cn.wolfcode.luowowo.comment.service.IItemCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.item.domain.UserItem;
import cn.wolfcode.luowowo.item.query.ItemQuery;
import cn.wolfcode.luowowo.item.service.IItemService;
import cn.wolfcode.luowowo.item.service.IUserItemService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;

@Controller
@RequestMapping("/itemshop")
public class ItemshopController {
    @Reference
    private IItemService itemService;
    @Reference
    private IUserItemService userItemService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IItemCommentService itemCommentService;

    @RequestMapping("")
    public String list(Model model,@UserParam UserInfo userInfo,@ModelAttribute("qo")ItemCommentQuery qo){
        if(userInfo != null){
            UserInfo newUserInfo = userInfoService.get(userInfo.getId());
            model.addAttribute("userInfo",newUserInfo);
        }
        Page page = itemCommentService.query(qo);
        model.addAttribute("page",page);
        return "itemshop/list";
    }

    @RequestMapping("/itemTempl")
    public String itemTempl(Model model, ItemQuery qo){
        //共享道具商店的页面
        PageInfo pageInfo = itemService.queryForList(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "itemshop/itemTempl";
    }

    @RequestMapping("myItemTempl")
    public String myItemTempl(Model model, ItemQuery qo,@UserParam UserInfo userInfo){
        //共享我的道具页面
        if(userInfo != null){
            PageInfo pageInfo = userItemService.queryForList(userInfo.getId(),qo);
            model.addAttribute("pageInfo",pageInfo);
        }else{
            PageInfo<UserItem> pageInfo = new PageInfo<UserItem>(Collections.EMPTY_LIST);
            model.addAttribute("pageInfo",pageInfo);
        }
        return "itemshop/myItemTempl";
    }

    @RequestMapping("/buyItem")
    @ResponseBody
    public Object buyItem(Model model,Long itemId,@UserParam UserInfo userInfo){
        //如果用户没有登录直接返回请先登录
        if (userInfo == null){
            return new AjaxResult(false,"请先登录");
        }
        userItemService.buyItem(userInfo.getId(), itemId);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/addComment")
    @ResponseBody
    public Object addComment(ItemComment comment,@UserParam UserInfo userInfo){
        if(userInfo == null){
            return new AjaxResult(false,"请先登录");
        }
        comment.setFromId(userInfo.getId());
        comment.setFromName(userInfo.getNickname());
        comment.setFromImg(userInfo.getHeadImgUrl());
        comment.setFromLevel(userInfo.getLevel());
        itemCommentService.save(comment);
        return AjaxResult.SUCCESS;
    }

    /*@RequestMapping("/commentTempl")
    public String commentTempl(Model model, @ModelAttribute("qo")ItemCommentQuery qo){
        Page page = itemCommentService.query(qo);
        model.addAttribute("page",page);
        return "itemshop/commentTempl";
    }*/

}
